package Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;

public class TestDemo4 {
    public static void main(String[] args) throws ParseException {

        //请使用代码实现计算你活了多少天，用JDK7和JDK8两种方式完成
        //JDK7
        //规则:只要对时间进行计算或者判断，都需要先获取当前时间的毫秒值
        //1.计算出生年月日的毫秒值
        String birthday1 = "2004年11月27日";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = sdf.parse(birthday1);
        long birthdayTime = date.getTime();

        //2.计算当前时间毫秒值
        long todayTime = System.currentTimeMillis();

        //3.计算天数并输出
        long time = todayTime - birthdayTime;
        int day = (int) (time / 1000 / 60 / 60 / 24 );
        System.out.println(day);

        //JDK8
        LocalDate birthday2 = LocalDate.of(2004, 11, 27);
        LocalDate today2 = LocalDate.now();

        long between = ChronoUnit.DAYS.between(birthday2, today2);
        System.out.println(between);
    }
}
